// index.js
import {
  get,
  post,
  put
} from '../../utils/request'
import {
  APIS
} from '../../utils/api'
import { timestampToTime } from '../../utils/util'
// 获取应用实例
const app = getApp()

Page({
  data: {
    shopArray: [],
    shopIds: [],
    shopList: [],
    shopInfo: {},
    ballsList: [],
    cardsList: [],
    systems: [
      {
        name: '空调',
        running: true
      },
      {
        name: '灯光',
        running: false
      }
    ],
    monitors: [
      {
        name: '一号监控',
        isCanUse: true
      },
      {
        name: '二号监控',
        isCanUse: true
      }
    ],
    onFree: 0,
    onStatus: 0,
    showPop: false,
    // 开台弹窗
    showOpenPop: false,
    // 选择固定时间关闭
    scheduleType: 0, // 1 小时  2 小时
    currentNum: 0,
    currTable: {},
    currType: 'ball',
    borderStyle: 'border: 6rpx #B3FC65 solid',
    // 定时开台时间输入
    inputValue: '',
  },
  onLoad() {
    const shopInfo = wx.getStorageSync('currShop');
    this.setData({
      shopInfo
    });
  },
  onShow() {
    this.getAlltable()
  },
  getAlltable() {
    get(APIS.controll, {shop: this.data.shopInfo.id})
      .then(res => {
        let {
          onStatus,
          onFree,
          ballsList,
          cardsList
        } = res.data;
        this.setData({
          onStatus,
          onFree,
          ballsList,
          cardsList
        })
      })
  },
  handleSwitchChange(e) {
    const { value } = e.detail;
    const { index } = e.currentTarget.dataset;
    this.setData({
      [`systems[${index}].running`]: value
    })
  },
  handleClose() {
    this.setData({
      showPop: false,
      showOpenPop: false
    })
  },
  hanldeOpen(e) {
    const { item, type } = e.currentTarget.dataset;

    // 判断是否为空闲中
    if (item.status === 0) {
      this.setData({
        showOpenPop: true,
        currTable: item,
        currType: type,
        currentNum: item.number
      })
    } else {
      this.setData({
        showPop: true,
        currTable: item,
        currType: type,
        currentNum: item.number
      })
    }
  },
  sureOpen() {
    const {currTable, currType, inputValue, scheduleType} = this.data;
    const {status, id} = currTable;

    // 计算end时间戳
    const currentTimestamp = Date.now();
    let end = 0;
    if (+inputValue || scheduleType) {
      end = (+inputValue + (scheduleType * 60)) * 60 * 1000 + currentTimestamp;
    }

    put(`${APIS.controll}`, {
      status,
      id,
      type: currType,
      // 增加end时间戳
      end
    })
      .then(res => {
        this.setData({
          showPop: false,
          showOpenPop: false,
        })
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }).finally(() => {
        this.getAlltable();
      })
  },
  handleShopInfo(e) {
    const shopInfo = e.detail;
    this.setData({
      shopInfo
    }, () => {
      this.getAlltable()
    });
  },
  handleJump() {
    wx.navigateTo({
      url: '/pages/vipDetail/index'
    })
  },
  // 固定时间点击
  scheduleClick(event) {
    const scheduleType = +event.currentTarget.dataset.type;
    this.setData({
      scheduleType: this.data.scheduleType === scheduleType ? 0 : scheduleType,
      inputValue: '',
    })
  },
  handleInputChange(event) {
    this.setData({
      inputValue: event.detail.value,
      scheduleType: 0,
    });
  }
})